from typing import List


class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                if nums[i] + nums[j] == target:
                    return [i, j]

    def twoSum2(self, nums: List[int], target: int) -> List[int]:
        for i, num in enumerate(nums):
            complement = target - num
            if complement in nums[i + 1:]:
                return [i, nums[i + 1:].index(complement) + (i + 1)]

    def twoSum3(self, nums: List[int], target: int) -> List[int]:
        nums_map = {}
        # 创建一个map，key为nums中的值，value为下标
        for i, num in enumerate(nums):
            # 找到补数
            complement = target - num
            # 判断是否在map中，在则返回下标
            if complement in nums_map:
                return [nums_map[complement], i]
            # 否则，将nums[i]作为key，i作为value
            nums_map[num] = i
        return []

    def twoSum4(self, nums: List[int], target: int) -> List[int]:
        nums_map = {}
        # 创建一个map，key为nums中的值，value为下标
        for i, num in enumerate(nums):
            # 判断是否在map中，在则返回下标
            if num in nums_map:
                return [nums_map[num], i]
            complement = target - num
            # 否则，将nums[i]作为key，i作为value
            nums_map[complement] = i
        return []


if __name__ == '__main__':
    s = Solution()
    # print(s.twoSum([2, 7, 11, 15], 9))
    # print(s.twoSum2([2, 17, 3, 9, 32, 6], 9))
    # print(s.twoSum3([2, 17, 3, 9, 32, 6], 9))
    print(s.twoSum4([2, 17, 3, 9, 32, 6], 9))
